home *** CD-ROM | disk | FTP | other *** search
- Path: citr.uq.oz.au!usenet
- From: Martin Pool <martin>
- Newsgroups: comp.lang.c++
- Subject: 'return' keyword in G++ (was Re: Parameterless functions)
- Date: 31 Jan 1996 23:22:35 GMT
- Organization: CiTR Pty Ltd
- Message-ID: <4eotjr$p9c@joppa.citr.uq.oz.au>
- References: <9601191115.aa06760@paris.ics.uci.edu> <4dpi25$vp@colossus.holonet.net> <4e7pqo$e4l@tech.cftnet.com>
- NNTP-Posting-Host: cooloola.citr.uq.oz.au
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.3_U1 sun4m)
- X-URL: news:4e7pqo$e4l@tech.cftnet.com
-
- wcowley@cftnet.com (Wes Cowley) wrote:
- >Russell Blackadar (russell@news.mdli.com) wrote:
- >: Raymond Klefstad, Ph.D. (klefstad@catalina.ICS.UCI.EDU) wrote:
- >:
- >: : Also, I noticed the following in some of the standard headers for gcc:
- >: : int foo(int x) return y
- >:
- >: It's not C++, and I wouldn't want my compiler to accept code like this.
- >: Are you sure this line actually compiles, or is it just a comment?
- >
- >If I recall correctly from the readmes, it's a g++ extension.
-
- It is in fact a g++ extension, and a useful one. The best explanation is an
- excerpt from the info manual (gcc 2.6.3).
-
- Martin
-
- -- info page begins --
-
- Named Return Values in C++
- ==========================
-
- GNU C++ extends the function-definition syntax to allow you to
- specify a name for the result of a function outside the body of the
- definition, in C++ programs:
-
- TYPE
- FUNCTIONNAME (ARGS) return RESULTNAME;
- {
- ...
- BODY
- ...
- }
-
- You can use this feature to avoid an extra constructor call when a
- function result has a class type. For example, consider a function
- `m', declared as `X v = m ();', whose result is of class `X':
-
- X
- m ()
- {
- X b;
- b.a = 23;
- return b;
- }
-
- Although `m' appears to have no arguments, in fact it has one
- implicit argument: the address of the return value. At invocation, the
- address of enough space to hold `v' is sent in as the implicit argument.
- Then `b' is constructed and its `a' field is set to the value 23.
- Finally, a copy constructor (a constructor of the form `X(X&)') is
- applied to `b', with the (implicit) return value location as the
- target, so that `v' is now bound to the return value.
-
- But this is wasteful. The local `b' is declared just to hold
- something that will be copied right out. While a compiler that
- combined an "elision" algorithm with interprocedural data flow analysis
- could conceivably eliminate all of this, it is much more practical to
- allow you to assist the compiler in generating efficient code by
- manipulating the return value explicitly, thus avoiding the local
- variable and copy constructor altogether.
-
- Using the extended GNU C++ function-definition syntax, you can avoid
- the temporary allocation and copying by naming `r' as your return value
- as the outset, and assigning to its `a' field directly:
-
- X
- m () return r;
- {
- r.a = 23;
- }
-
- The declaration of `r' is a standard, proper declaration, whose effects
- are executed *before* any of the body of `m'.
-
- Functions of this type impose no additional restrictions; in
- particular, you can execute `return' statements, or return implicitly by
- reaching the end of the function body ("falling off the edge"). Cases
- like
-
- X
- m () return r (23);
- {
- return;
- }
-
- (or even `X m () return r (23); { }') are unambiguous, since the return
- value `r' has been initialized in either case. The following code may
- be hard to read, but also works predictably:
-
- X
- m () return r;
- {
- X b;
- return b;
- }
-
- The return value slot denoted by `r' is initialized at the outset,
- but the statement `return b;' overrides this value. The compiler deals
- with this by destroying `r' (calling the destructor if there is one, or
- doing nothing if there is not), and then reinitializing `r' with `b'.
-
- This extension is provided primarily to help people who use
- overloaded operators, where there is a great need to control not just
- the arguments, but the return values of functions. For classes where
- the copy constructor incurs a heavy performance penalty (especially in
- the common case where there is a quick default constructor), this is a
- major savings. The disadvantage of this extension is that you do not
- control when the default constructor for the return value is called: it
- is always called at the beginning.
-
-